package com.group4.datafactorybackend.controller;

import com.group4.datafactorybackend.common.R;
import com.group4.datafactorybackend.entity.DTO.BatchReleaseOrOutageDTO;
import com.group4.datafactorybackend.entity.DTO.CreateOrUpdateStopwatchDTO;
import com.group4.datafactorybackend.entity.DTO.StopwatchParameterDTO;
import com.group4.datafactorybackend.service.StopwatchManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 卑微小峰
 * @date 2022/8/1
 * @description 码表管理控制器类
 */
@Api(tags = "码表管理")
@RestController
@RequestMapping("/stopwatch")
public class StopwatchManagementController {

    private final StopwatchManagementService stopwatchManagementService;

    @Autowired
    public StopwatchManagementController(StopwatchManagementService stopwatchManagementService) {
        this.stopwatchManagementService = stopwatchManagementService;
    }

    /**
     * @author 邹代鑫
     * @param createOrUpdateStopwatchDTO 新增、修改码表接收参数实体类
     * @return R
     * @description 新增码表
     */
    @ApiOperation("新增码表")
    @PostMapping("/post/addStopwatch")
    public R<?> addStopwatch(@RequestBody @Validated CreateOrUpdateStopwatchDTO createOrUpdateStopwatchDTO){
        return stopwatchManagementService.addStopwatch(createOrUpdateStopwatchDTO);
    }

    /**
     * @author 邹代鑫
     * @param createOrUpdateStopwatchDTO 新增、修改码表接收参数实体类
     * @return R
     * @description 编辑码表
     */
    @ApiOperation("编辑码表")
    @PutMapping("/update/updateStopwatch")
    public R<?> updateStopwatch(@RequestBody @Validated CreateOrUpdateStopwatchDTO createOrUpdateStopwatchDTO) {
        return stopwatchManagementService.updateStopwatch(createOrUpdateStopwatchDTO);
    }

    /**
     * @author 卑微小峰
     * @param batchReleaseOrOutageDTO （批量）发布/停用接收参数实体类
     * @return R
     * @description （批量）发布/停用码表
     */
    @ApiOperation("发布/停用码表")
    @PutMapping("/update/releaseOrOutage")
    public R<?> releaseOrOutage(@RequestBody @Validated BatchReleaseOrOutageDTO batchReleaseOrOutageDTO) {
        return stopwatchManagementService.releaseOrOutage(batchReleaseOrOutageDTO);
    }

    /**
     * @author 章珑赢
     * @param parameterDTO 查询码表列表条件实体类
     * @return R
     * @description 查询码表列表
     */
    @ApiOperation("查询码表列表")
    @PostMapping("/post/stopwatchList")
    public R<?> stopwatchList(@RequestBody @Validated StopwatchParameterDTO parameterDTO){
      return stopwatchManagementService.stopwatchList(parameterDTO);
    }

    /**
     * @author 章珑赢
     * @param stopwatchCode 码表编号
     * @return R
     * @description 删除码表
     */
    @ApiOperation("删除码表")
    @DeleteMapping("/delete/stopwatchList/{stopwatchCode}")
    public R<?> deleteStopwatch(@PathVariable("stopwatchCode")String stopwatchCode){
       return stopwatchManagementService.deleteByStopwatch(stopwatchCode);
    }

    /**
     * @author 李泓静
     * @param stopwatchCode 码表编号
     * @return R
     * @description 查看码表详情
     */
    @ApiOperation("查看码表详情")
    @GetMapping("/get/getStopwatchDetail/{stopwatchCode}")
    public R<?> getStopwatchDetail(@PathVariable("stopwatchCode")String stopwatchCode) {
        return stopwatchManagementService.getStopwatchDetail(stopwatchCode);
    }

    /**
     * @author 邹代鑫
     * @param response 请求响应参数
     * @return R
     * @description 导出标准码表
     */
    @ApiOperation("导出标准码表")
    @GetMapping("/get/exportExcel")
    public R<?> exportExcel(HttpServletResponse response){
        return stopwatchManagementService.export(response);
    }

    /**
     * @author 邹代鑫
     * @param file 文件
     * @return R
     * @description 导入表格
     */
    @ApiOperation("导入表格")
    @PostMapping("/post/importExcel")
    public R<?> importExcel(@RequestParam("file")MultipartFile file){
        return this.stopwatchManagementService.importExcel(file);
    }
}
